package com.fhx.core.base

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import androidx.fragment.app.Fragment
import com.fhx.core.R
import com.fhx.core.utils.*

abstract class BaseFragment : Fragment() {

    private var fragmentView: View? = null
    protected var mProgressView: View? = null
    private val statusBarHeight: Int by lazy { ScreenUtil.getStatusBarHeight(requireContext()) }
    private var isInited = false//是否初始化过
    protected var rootView: ViewGroup? = null

    override fun onAttach(context: Context) {
        super.onAttach(AppLanguageUtils.attachBaseContext(context, getAppLanguage(context)!!))
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val language = getAppLanguage(requireContext())
        AppLanguageUtils.changeAppLanguage(
            requireContext(),
            AppLanguageUtils.getSupportLanguage(language)
        )

        if (!isInited) {
            isInited = true
            main()
        }
    }

    private fun getAppLanguage(context: Context): String? {
        return SharePreferencesUtil.getString(
            context,
            AppLanguageUtils.LANGUAGE,
            AppLanguageUtils.CHINESE
        )
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        if (fragmentView == null) {
            fragmentView = layoutInflater.inflate(getLayoutId(), container, false)
        }
        rootView = requireActivity().window.decorView as ViewGroup
        initProgressView()
        return fragmentView
    }

    override fun onDestroyView() {
        super.onDestroyView()
        if (fragmentView != null) {
            val parent = fragmentView!!.parent as? ViewGroup
            parent?.removeView(fragmentView)
        }
    }

    private fun initProgressView() {
        if (mProgressView == null) {
            mProgressView = layoutInflater.inflate(R.layout.loading_layout, rootView, false)
            mProgressView?.setOnClickListener(null)
            showProgress(false)
            rootView?.addView(mProgressView)
        }
    }

    abstract fun getLayoutId(): Int

    open fun main() {

    }

    fun setTopPadding(view: View) {
        view.setPadding(0, statusBarHeight, 0, 0)
    }

    fun showToast(text: CharSequence) {
        ToastUtils.show(text)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EventBusManager.register(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBusManager.unregister(this)
    }

    protected fun showProgress(show: Boolean) {
        mProgressView?.visibility = if (show) View.VISIBLE else View.GONE
    }


//    private var lastClickTime: Long = 0

    private val lastClickTimeMap: HashMap<Int, Long> by lazy { HashMap<Int, Long>() }

    @Synchronized
    fun isFastClick(view: View?): Boolean {
        if (view == null) {
            return false
        }
        val time = System.currentTimeMillis()
        val lastClickTime = lastClickTimeMap[view.id]
        val timeDelta = time - (lastClickTime ?: 0L)
        return if (timeDelta in 0..300) {
            true
        } else {
//            lastClickTime = time
            lastClickTimeMap[view.id] = time
            false
        }
    }


    private var lastClickTime: Long = 0

    @Synchronized
    fun isFastClick(): Boolean {
        val time = System.currentTimeMillis()
        val timeDelta = time - lastClickTime
        return if (timeDelta in 0..300) {
            true
        } else {
            lastClickTime = time
            false
        }
    }

    /**
     * 隐藏软键盘
     */
    open fun hideSoftKeyboard(activity: Activity?) {
        val view: View? = activity?.currentFocus
        if (view != null) {
            val inputMethodManager: InputMethodManager =
                activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
            inputMethodManager.hideSoftInputFromWindow(
                view.windowToken,
                InputMethodManager.HIDE_NOT_ALWAYS
            )
        }
    }
}